import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login,
        meta: { title: '登录' }
    },
    {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        redirect: '/dashboard/overview',
        children: [
            {
                path: 'overview',
                name: 'DashboardOverview',
                component: () => import('../views/dashboard/Overview.vue'),
                meta: { title: '系统概览' }
            },
            {
                path: 'users',
                name: 'UserManagement',
                component: () => import('../views/dashboard/UserManagement.vue'),
                meta: { title: '用户管理' }
            },
            {
                path: 'dynamics',
                name: 'DynamicsManagement',
                component: () => import('../views/dashboard/DynamicsManagement.vue'),
                meta: { title: '行业动态管理' }
            },
            {
                path: 'courses',
                name: 'CourseManagement',
                component: () => import('../views/dashboard/CourseManagement.vue'),
                meta: { title: '课程管理' }
            },
            {
                path: 'meetings',
                name: 'MeetingManagement',
                component: () => import('../views/dashboard/MeetingManagement.vue'),
                meta: { title: '会议管理' }
            }
        ]
    },
    {
        path: '/analytics',
        name: 'AnalyticsDashboard',
        component: () => import('../views/AnalyticsDashboard.vue'),
        meta: { title: '智能数据分析' }
    },
    {
        path: '/workflow',
        name: 'WorkflowAutomation',
        component: () => import('../views/WorkflowAutomation.vue'),
        meta: { title: '工作流自动化' }
    },
    {
        path: '/assistant',
        name: 'AIChatAssistant',
        component: () => import('../views/AIChatAssistant.vue'),
        meta: { title: 'AI 助手' }
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const isLoggedIn = localStorage.getItem('isLoggedIn')

    if (to.meta.requiresAuth && !isLoggedIn) {
        next('/')
    } else if (to.path === '/' && isLoggedIn) {
        next('/dashboard')
    } else {
        next()
    }
})

export default router